package com.it.dao;

import com.it.domain.Teacher;

import java.util.ArrayList;
import java.util.List;

public class TeacherDao {
    //模拟数据库
    public static ArrayList<Teacher> teachers = new ArrayList();

    //判断是否存在
    public boolean isExist(String id) {
        for (Teacher teacher : teachers) {
            if (teacher.getTeacherId().equals(id)) {
                return true;
            }
        }
        return false;
    }

    //添加教师
    public boolean add(Teacher teacher) {
        return teachers.add(teacher);
    }

    //查询所有
    public List<Teacher> findAll() {
        if (teachers.size() > 0) {
            return teachers;
        }
        System.out.println("暂无教师信息");
        return teachers;
    }

    //删除
    public boolean delete(String id) {
        //判断是否存在
        if (!isExist(id)) {
            System.out.println("该教师不存在");
            return false;
        }
        for (Teacher teacher : teachers) {
            if (teacher.getTeacherId().equals(id)) {
                teachers.remove(teacher);
                return true;
            }
        }
        return false;
    }

    //修改
    public boolean update(Teacher teacher) {
        if (!isExist(teacher.getTeacherId())) {
            System.out.println("该教师不存在");
            return false;
        }
        for (Teacher t : teachers) {
            if (t.getTeacherId().equals(teacher.getTeacherId())) {
                t.setName(teacher.getName());
                t.setAge(teacher.getAge());
                t.setGender(teacher.getGender());
                t.setSubject(teacher.getSubject());
                return true;
            }
        }
        return false;
    }
}
